package com.eshop.modules.shop.rest;

import org.springframework.web.bind.annotation.DeleteMapping;
import com.eshop.modules.aop.ForbidSubmit;
import java.io.Serializable;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.PostMapping;
import com.eshop.utils.SecurityUtils;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.validation.annotation.Validated;
import com.eshop.modules.shop.domain.Material;
import io.swagger.annotations.ApiOperation;
import com.eshop.logging.aop.log.Log;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.data.domain.Pageable;
import com.eshop.modules.shop.service.dto.MaterialQueryCriteria;
import com.eshop.modules.shop.service.MaterialService;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import io.swagger.annotations.Api;

@Api(tags = { "商城:素材管理管理" })
@RestController
@RequestMapping({ "/api/material" })
public class MaterialController
{
    private final MaterialService materialService;
    
    public MaterialController(final MaterialService materialService) {
        this.materialService = materialService;
    }
    
    @GetMapping({ "/page" })
    @Log("查询素材管理")
    @ApiOperation("查询素材管理")
    public ResponseEntity<Object> getYxMaterials(final MaterialQueryCriteria criteria, final Pageable pageable) {
        return new ResponseEntity(this.materialService.queryAll(criteria, pageable), HttpStatus.OK);
    }
    
    @PostMapping
    @Log("新增素材管理")
    @ApiOperation("新增素材管理")
    public ResponseEntity<Object> create(@Validated @RequestBody final Material resources) {
        resources.setCreateId(SecurityUtils.getUsername());
        return new ResponseEntity(this.materialService.save(resources), HttpStatus.CREATED);
    }
    
    @PutMapping
    @Log("修改素材管理")
    @ApiOperation("修改素材管理")
    public ResponseEntity<Object> update(@Validated @RequestBody final Material resources) {
        this.materialService.saveOrUpdate(resources);
        return new ResponseEntity(HttpStatus.NO_CONTENT);
    }
    
    @ForbidSubmit
    @Log("删除素材管理")
    @ApiOperation("删除素材管理")
    @DeleteMapping({ "/{id}" })
    public ResponseEntity<Object> deleteAll(@PathVariable final String id) {
        this.materialService.removeById((Serializable)id);
        return new ResponseEntity(HttpStatus.OK);
    }
}
